package com.efast.dmom.mgmt.openws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cafeauto.ws.Message;
import com.efast.cafe.commponent.es.webservice.annotation.CafeWSService;
import com.efast.cafe.commponent.es.webservice.base.WSAbstractAdapter;
import com.efast.cafe.commponent.es.webservice.utils.W3CElementUtil;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.dmom.mgmt.bean.mes.testline.ABSData;
import com.efast.dmom.mgmt.bean.mes.testline.ABSDataBean;
import com.efast.dmom.mgmt.bean.mes.testline.ABSDataBizContent;
import com.efast.dmom.mgmt.bean.mes.testline.ABSInboundDataSet;
import com.efast.dmom.mgmt.bean.mes.testline.TestLineDataLogBean;
import com.efast.dmom.mgmt.service.mes.testline.impl.TestLineService;

@CafeWSService(bizCode = "ABS_Test")
public class ABSDataService extends WSAbstractAdapter {

	@Override
	public Message handleMessage(Message message) {

		logger.info("Received ABS data from: " + message.getBIZ_CODE());

		ABSDataBizContent content = W3CElementUtil.elementUnmarshalToObj(message.getBIZ_CONTENT(),
				ABSDataBizContent.class);
		ABSInboundDataSet inboundData = content.getaBSInboundDataSet();
		List<ABSData> dataList = inboundData.getAbsData();
		
		if(dataList != null && dataList.size() >0){
			
			List<ABSDataBean> list = new ArrayList<ABSDataBean>();
			for (ABSData data : dataList) {

				ABSDataBean dataBean = new ABSDataBean();

				dataBean.setVin(data.getVin());
				dataBean.setaBSResult(data.getaBSResult());
				dataBean.setAlarmLightOff(data.getAlarmLightOff());
				dataBean.setAlarmLightsOn(data.getAlarmLightsOn());
				dataBean.setDynamicInspectionResult(data.getDynamicInspectionResult());
				dataBean.seteCUNumber(data.geteCUNumber());
				dataBean.setOneAxisLeftSpeedMax(data.getOneAxisLeftSpeedMax());
				dataBean.setOneAxisLeftSpeedMin(data.getOneAxisLeftSpeedMin());
				dataBean.setOneAxisLeftValveAttenuationRate(data.getOneAxisLeftValveAttenuationRate());
				dataBean.setOneAxisLeftValveRecoveryRate(data.getOneAxisLeftValveRecoveryRate());
				dataBean.setOneAxisRightSpeedMax(data.getOneAxisRightSpeedMax());
				dataBean.setOneAxisRightSpeedMin(data.getOneAxisRightSpeedMin());
				dataBean.setOneAxisRightValveAttenuationRate(data.getOneAxisRightValveAttenuationRate());
				dataBean.setOneAxisRightValveRecoveryRate(data.getOneAxisRightValveRecoveryRate());
				dataBean.setSelfCheckingResult(data.getSelfCheckingResult());
				dataBean.setSupplier(data.getSupplier());
				dataBean.setTwoAxisLeftSpeedMax(data.getTwoAxisLeftSpeedMax());
				dataBean.setTwoAxisLeftSpeedMin(data.getTwoAxisLeftSpeedMin());
				dataBean.setTwoAxisLeftValveAttenuationRate(data.getTwoAxisLeftValveAttenuationRate());
				dataBean.setTwoAxisLeftValveRecoveryRate(data.getTwoAxisLeftValveRecoveryRate());
				dataBean.setTwoAxisRightSpeedMax(data.getTwoAxisRightSpeedMax());
				dataBean.setTwoAxisRightSpeedMin(data.getTwoAxisRightSpeedMin());
				dataBean.setWriteTypeCode(data.getWriteTypeCode());
				dataBean.setTwoAxisRightValveRecoveryRate(data.getTwoAxisRightValveRecoveryRate());
				dataBean.setTwoAxisRightValveAttenuationRate(data.getTwoAxisRightValveAttenuationRate());
				dataBean.setWriteVINCode(data.getWriteVINCode());
				dataBean.setTestNumber(data.getTestNumber());

				dataBean.setCreateTime(new Date());
				dataBean.setCreateUser(message.getBIZ_CODE());

				list.add(dataBean);
			}

			TestLineService service = SpringContextUtils.getBean(TestLineService.class);
			service.insertABSData(list);

			TestLineDataLogBean logBean = new TestLineDataLogBean();
			logBean.setVin(dataList.get(0).getVin());
			logBean.setStatus("保存");
			logBean.setReceivedTime(new Date());
			logBean.setSource(message.getMESSAGE_FROM());
			logBean.setDestination(message.getMESSAGE_TO());
			
			service.insertTestLineLog(logBean);
		}

		message.setBIZ_CONTENT(null);
		message.setMESSAGE_FROM("MES");
		message.setMESSAGE_TO("ABS_Test");
		message.setRET_CODE("200");
		message.setRET_CODE_DESC("Successful");
		return message;

	}

}
